//
//  MovedImageView.m
//  costa-postcards
//
//  Created by 黄 鹏霄 on 12-12-19.
//  Copyright (c) 2012年 test. All rights reserved.
//

#import "MovedImageView.h"
#import <QuartzCore/QuartzCore.h>


@implementation MovedImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setUserInteractionEnabled:YES];
        active = YES;

        UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handelPan:)];
        [self addGestureRecognizer:panGes];
        panGes.delegate = self;
        [panGes release];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:tapGes];
        tapGes.delegate = self;
        [tapGes release];
        
        UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
        [self addGestureRecognizer:rotateGes];
        rotateGes.delegate = self;
        [rotateGes release];
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/



#pragma mark - image view delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    x = self.frame.origin.x;
    y = self.frame.origin.y;
    
    if (!active) {
        return NO;
    }
    
    return YES;
}

#pragma mark - handle pan gesture
- (void)handelPan:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *superView = [self superview];
    CGPoint curPoint = [gestureRecognizer translationInView:superView];
    [self setFrame:CGRectMake(curPoint.x + x, curPoint.y + y, self.frame.size.width, self.frame.size.height)];
}

#pragma mark - handle tap gesture
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    [self.layer setBorderColor:[[UIColor yellowColor] CGColor]];
    [self.layer setBorderWidth:2.0];
}

#pragma mark - rotate gesture
- (void)handleRotate:(UIRotationGestureRecognizer *)rotateRecognizer
{
    
}

@end
